
//
//  SNXYSelectedNumberView.m
//  ShenNongXingYun
//
//  Created by 冰冰 on 2018/12/1.
//  Copyright © 2018 gaomeng. All rights reserved.
//

#import "SNXYSelectedNumberView.h"

@interface SNXYSelectedNumberView ()<UIPickerViewDelegate,UIPickerViewDataSource>

@property (nonatomic, strong)UIPickerView *pickerView;
@property (nonatomic, assign)NSInteger currentIndex;

@property (nonatomic, strong)UIView *view;
@property (nonatomic, strong)UIButton *sureButton;
@property (nonatomic, strong)UIButton *cancelButton;

@end

@implementation SNXYSelectedNumberView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.backgroundColor = [[UIColor colorWithHexString:@"020202"]colorWithAlphaComponent:0.57];
        
        self.numberArray = @[@"整车",@"半车",@"1/3车",@"1/4车"];
        self.currentIndex = 0;
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clickCancelButton)];
        [self addGestureRecognizer:tap];
        
        self.view = [[UIView alloc]initWithFrame:CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, 43)];
        self.view.backgroundColor = [UIColor whiteColor];
        [self addSubview:self.view];
        
        self.sureButton = [[UIButton alloc]initWithFrame:CGRectMake(SCREEN_WIDTH-60, 0, 60, 43-0.5)];
        [self.sureButton addTarget:self action:@selector(clickOkButton) forControlEvents:UIControlEventTouchUpInside];
        self.sureButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
        [self.sureButton setTitle:@"确定" forState:UIControlStateNormal];
        [self.sureButton setTitleColor:KBlackColor_222222 forState:UIControlStateNormal];
        [self.sureButton setTitleColor:KBlackColor_222222 forState:UIControlStateHighlighted];
        self.sureButton.titleLabel.font = FONT_SIZE(15);
        [self.view addSubview:self.sureButton];
        
        self.cancelButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 60, 43-0.5)];
        [self.cancelButton addTarget:self action:@selector(clickCancelButton) forControlEvents:UIControlEventTouchUpInside];
        [self.cancelButton setTitle:@"取消" forState:UIControlStateNormal];
        [self.cancelButton setTitleColor:KBlackColor_222222 forState:UIControlStateNormal];
        [self.cancelButton setTitleColor:KBlackColor_222222 forState:UIControlStateHighlighted];
        self.cancelButton.titleLabel.font = FONT_SIZE(15);
        [self.view addSubview:self.cancelButton];
        
        UIView *line = [[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(self.sureButton.frame), SCREEN_WIDTH, 0.5)];
        line.backgroundColor = KGrayColor_ededed;
        [self.view addSubview:line];
        
        
        //父类 UIView
        _pickerView = [[UIPickerView alloc]initWithFrame:CGRectMake(0, SCREEN_HEIGHT+43, SCREEN_WIDTH, 200)];
        _pickerView.backgroundColor = [UIColor whiteColor];
        _pickerView.dataSource = self;
        _pickerView.delegate = self;
        [self addSubview:_pickerView];
        
        
        [UIView animateWithDuration:0.4 animations:^{
            
            NSString *version = [UIDevice currentDevice].systemVersion;
            if (version.doubleValue>=9.0) {
                self.view.frame = CGRectMake(0,frame.size.height-200-43, frame.size.width, 43);
            }else{
                self.view.frame = CGRectMake(0,frame.size.height-200-43+64, frame.size.width, 43);
            }
            self.pickerView.frame = CGRectMake(0, CGRectGetMaxY(self.view.frame), SCREEN_WIDTH, 200);
        }];
        self.frame = frame;
        
    }
    return self;
    
}



-(void)setNumberArray:(NSArray *)numberArray
{
    _numberArray = numberArray;
    [self.pickerView reloadAllComponents];
}

-(void)clickOkButton
{
    [UIView animateWithDuration:0.4 animations:^{
        self.view.frame = CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, 43);
        self.pickerView.frame = CGRectMake(0, SCREEN_HEIGHT+43, SCREEN_WIDTH, 200);
    }];
    self.frame = CGRectMake(0, 0, 0, 0);
    
    if (self.clickIndexBlock) {
        self.clickIndexBlock(self.currentIndex);
    }
    
    self.currentIndex = 0;
}

-(void)clickCancelButton
{
    [UIView animateWithDuration:0.4 animations:^{
        self.view.frame = CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, 43);
        self.pickerView.frame = CGRectMake(0, SCREEN_HEIGHT+43, SCREEN_WIDTH, 200);
    }];
    self.frame = CGRectMake(0, 0, 0, 0);
    
    self.currentIndex = 0;
}

//设置列的数目
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}

//设置某一列的行数目
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return self.numberArray.count;
}

//设置某列某行的标题
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    return self.numberArray[row];
}

//选中某列的某一行会调用的方法
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    self.currentIndex = row;
    //刷新
    [pickerView  reloadAllComponents];
    //修改选中行 为 0行
    [pickerView selectRow:row inComponent:component animated:YES];
}

- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{
    return 40;
}

//自定义picker
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
    //设置分割线的颜色
    for(UIView *singleLine in pickerView.subviews)
    {
        if (singleLine.frame.size.height < 1)
        {
            singleLine.backgroundColor = KGrayColor_ededed;
        }
    }
    
    UILabel *pickerLabel = (UILabel*)view;
    if (!pickerLabel) {
        pickerLabel = [[UILabel alloc]init];
        pickerLabel.font = [UIFont systemFontOfSize:15];
        pickerLabel.adjustsFontSizeToFitWidth = YES;
        pickerLabel.textAlignment = NSTextAlignmentCenter;
        pickerLabel.textColor = KBlackColor_222222;
    }
    pickerLabel.text = [self pickerView:pickerView titleForRow:row forComponent:component];
    
    return pickerLabel;
    
}


@end
